/**
 * Created by zy on 2016/6/23.
 * 维修流程管理 - 管体缺陷
 */

;(function ($, window, document, undefined) {

    // 初始化流程
    DyyBase.ajax(defectApi.url_flow_list(), {}, function (dataset) {
        dataFormat.getFlowList(dataset);
    });

    // 数据格式化
    var dataFormat = {
        // 获得流程列表
        getFlowList: function (data) {
            var result = data.result;

            // 流程
            var pd_list = result.pd_list;
            // 节点
            var node_list = result.node_list;
            // 对应表
            var pi_list = result.pi_list;

            var pd = [], pi = [], node = [];

            for (var i in pd_list) {
                var temp_pd = {};
                temp_pd.id = pd_list[i].id;
                temp_pd.name = pd_list[i].name;
                temp_pd.delete = pd_list[i].allowDeleted;
                pd.push(temp_pd);
            }

            for (var j in node_list) {
                var temp_node = {};
                temp_node.id = node_list[j].id;
                temp_node.name = node_list[j].name;
                temp_node.necessary = node_list[j].necessary;
                temp_node.need = node_list[j].need;
                node.push(temp_node);
            }

            for (var k in pi_list) {
                var temp_pi = {};
                temp_pi.DEF_ID = pi_list[k].DEF_ID;
                temp_pi.NODE_ID = pi_list[k].NODE_ID;
                temp_pi.NODE_NAME = pi_list[k].NODE_NAME;
                temp_pi.IS_PERFORM = pi_list[k].IS_PERFORM;
                temp_pi.NECESSARY = pi_list[k].NECESSARY;
                temp_pi.PI_ID = pi_list[k].PI_ID;
                temp_pi.SORT = pi_list[k].SORT;
                pi.push(temp_pi);
            }

            var flow = [];
            for (var l in pd) {
                var pd_obj = {};

                pd_obj.id = pd[l].id;
                pd_obj.name = pd[l].name;
                pd_obj.delete = pd[l].delete;
                pd_obj.node = [];

                for (var m in pi) {
                    if (pd[l].id == pi[m].DEF_ID) {
                        var son = {};
                        son.NODE_ID = pi[m].NODE_ID;
                        son.DEF_ID = pi[m].DEF_ID;
                        son.NODE_NAME = pi[m].NODE_NAME;
                        son.IS_PERFORM = pi[m].IS_PERFORM;
                        son.NECESSARY = pi[m].NECESSARY;
                        son.PI_ID = pi[m].PI_ID;
                        son.SORT = pi[m].SORT;
                        pd_obj.node.push(son);
                    }
                }
                flow.push(pd_obj);
            }

            var $line_box = $('div.zy-liucheng-box');
            for (var x in flow) {
                var $line = '';
                if (flow[x].delete == '0') {
                    $line = $('<div class="zy-liucheng-line">'
                        + '<div class="zy-liucheng-inner">'
                        + '<p class="zy-liucheng-bar">'
                        + '<span class="l"><em id="' + flow[x].id + '">' + flow[x].name + '</em></span>'
                        + '<span class="r"><button class="zy-button-s zy-create-s disabled" data-act="update" type="button"><i class="zy-create-icon"></i>保存</button>&nbsp;&nbsp;<button class="zy-button-s zy-delete-s" data-pid="' + flow[x].id + '"><i class="zy-delete-icon"></i>删除</button></span>'
                        + '</p>'
                        + '<p class="zy-liucheng-p"></p></div></div>');
                } else {
                    $line = $('<div class="zy-liucheng-line">'
                        + '<div class="zy-liucheng-inner">'
                        + '<p class="zy-liucheng-bar">'
                        + '<span class="l"><em id="' + flow[x].id + '">' + flow[x].name + '</em></span>'
                        + '<span class="r"><button class="zy-button-s zy-create-s disabled" data-act="update" type="button"><i class="zy-create-icon"></i>保存</button></span>'
                        + '</p>'
                        + '<p class="zy-liucheng-p"></p></div></div>');
                }

                var $liucheng = $line.find('p.zy-liucheng-p');

                for (var y in flow[x].node) {
                    var $inner = $('<span><label class="zy-chk-label"><i></i><em></em></label></span>');

                    $inner.find('i').prop('id', flow[x].node[y].NODE_ID);
                    $inner.find('i').attr('data-name', flow[x].node[y].NODE_NAME);
                    $inner.find('i').attr('data-sort', '0');
                    $inner.find('i').attr('data-perform', flow[x].node[y].IS_PERFORM);
                    $inner.find('i').attr('data-pid', flow[x].node[y].PI_ID);
                    $inner.find('i').attr('data-change', 'false');

                    if (flow[x].node[y].NODE_ID == '1E3290704BF1749EE0530E0AA8C00077') {
                        var a = Dyy.htmlDecode(flow[x].name);
                        var b = a.length > 8 ? a.substr(0, 8) + '...' : a;
                        $inner.find('em').html(b).attr('title', a);
                    } else {
                        $inner.find('em').html(flow[x].node[y].NODE_NAME).attr('title', flow[x].node[y].NODE_NAME);
                    }

                    if (y == 0) {
                        $inner.find('label').addClass('zy-lc-l-bg');
                        $inner.append('<kbd class="zy-lc-bg w-4"></kbd>');
                    } else if (y == flow[x].node.length - 1) {
                        $inner.find('label').addClass('zy-lc-r-bg');
                    } else {
                        $inner.append('<kbd class="zy-lc-bg w-4"></kbd>');
                    }
                    if (flow[x].node[y].NECESSARY == 0) {
                        $inner.find('i').addClass('chk');
                        if (flow[x].node[y].IS_PERFORM == 1) {
                            $inner.find('i').addClass('active');
                        }
                    }
                    $liucheng.append($inner);
                }
                $line_box.append($line);
            }

            // 新增流程
            var $org_line = $('div.zy-original-line');
            var $org_inner = $('<div class="zy-liucheng-inner">'
                + '<p class="zy-liucheng-bar">'
                + '<span class="l zy-input-inner w-31"><input type="text" placeholder="请输入维修流程名称" class="zy-i-input" maxlength="15"></span>'
                + '<span class="r"><button class="zy-button-s zy-create-s" data-act="create" type="button"><i class="zy-create-icon"></i>保存</button>&nbsp;&nbsp;<button class="zy-button-s zy-delete-s" type="button"><i class="zy-delete-icon"></i>删除</button></span>'
                + '</p>'
                + '<p class="zy-liucheng-p"></p></div>');
            var $org_liucheng = $org_inner.find('p.zy-liucheng-p');
            for (var o in node) {
                var $inner_org = $('<span><label class="zy-chk-label"><i></i><em></em></label></span>');
                $inner_org.find('em').text(node[o].name);
                $inner_org.find('i').prop('id', node[o].id);
                $inner_org.find('i').attr('data-name', node[o].name);
                $inner_org.find('i').attr('data-sort', '0');
                $inner_org.find('i').attr('data-perform', node[o].necessary);
                $inner_org.find('i').attr('data-change', 'false');

                if (o == 0) {
                    $inner_org.find('label').addClass('zy-lc-l-bg');
                    $inner_org.append('<kbd class="zy-lc-bg w-4"></kbd>');
                } else if (o == node.length - 1) {
                    $inner_org.find('label').addClass('zy-lc-r-bg');
                } else {
                    $inner_org.append('<kbd class="zy-lc-bg w-4"></kbd>');
                }
                if (node[o].necessary == 0) {
                    $inner_org.find('i').addClass('chk');
                }
                $org_liucheng.append($inner_org);
            }

            $org_line.append($org_inner);
        },
        // 保存一个流程
        getFlow: function (obj) {
            var $parent_div = obj.parent('span').parent('p.zy-liucheng-bar').parent('div.zy-liucheng-inner');
            var $flow_p = $parent_div.find('p.zy-liucheng-p');
            var $bar = $parent_div.find('p.zy-liucheng-bar');
            var flow_id = '', flow_name = '', $data_i = '', node_str = '';

            if (obj.attr('data-act') == 'update') {
                flow_id = $bar.find('em').prop('id');
                flow_name = $bar.find('em').text();
                $data_i = $flow_p.find('i');
                node_str = 'processDefinition.name=' + flow_name + '&processDefinition.id=' + flow_id;

                $data_i.each(function () {
                    var $this_i = $(this);
                    var node_id = $this_i.prop('id');
                    var node_name = $this_i.attr('data-name');
                    var node_sort = $this_i.attr('data-sort');
                    var node_perform = $this_i.attr('data-perform');
                    var node_pid = $this_i.attr('data-pid');
                    node_str += '&processInstance.nodeId=' + node_id
                        + '&processInstance.nodeName=' + node_name
                        + '&processInstance.sort=' + node_sort
                        + '&processInstance.isPerform=' + node_perform
                        + '&processInstance.id=' + node_pid;
                });
                return node_str;

            } else if (obj.attr('data-act') == 'create') {
                var $input = $bar.find('input.zy-i-input');
                flow_name = $.trim($input.val());
                if (flow_name) {
                    if (flow_name.length > 15) {
                        $input.TIP({content: '维修流程名称不能超过14个字符！', scroll: false});
                        return false;
                    }
                } else {
                    $input.TIP({content: '维修流程名称不能为空！', scroll: false});
                    return false;
                }

                $data_i = $flow_p.find('i');
                var arr = [];
                arr.push({name: 'processDefinition.name', value: flow_name});
                $data_i.each(function () {
                    var $this_i = $(this);
                    var node_id = $this_i.prop('id');
                    var node_name = $this_i.attr('data-name');
                    var node_sort = $this_i.attr('data-sort');
                    var node_perform = $this_i.attr('data-perform');
                    arr.push({name: 'processInstance.nodeId', value: node_id});
                    arr.push({name: 'processInstance.nodeName', value: node_name});
                    arr.push({name: 'processInstance.sort', value: node_sort});
                    arr.push({name: 'processInstance.isPerform', value: node_perform});
                });
                return arr;
            }
        }
    };

    var $original_line = $('div.zy-original-line');
    // 新增流程 - 保存
    $original_line.on('click', 'button.zy-create-s', function () {
        var $this_btn = $(this);
        var post_data = dataFormat.getFlow($this_btn);
        if (post_data) {
            DyyBase.ajax(defectApi.url_flow_create(), post_data, function (dataset) {
                if (dataset.bool) {
                    $this_btn.MSG({
                        'type': 'success',
                        'content': '新增维修流程成功！',
                        'yesBtnAct': function () {
                            DyyBase.pageReload();
                        },
                        'closeBtnAct': function () {
                            DyyBase.pageReload();
                        }
                    });
                }
            });
        }
    });

    // 新增流程 - 检测名称
    $original_line.on('keyup', 'input.zy-i-input', function () {
        var $this_input = $(this);
        var $parent_inner = $this_input.parent('span.zy-input-inner').parent('p.zy-liucheng-bar').parent('div.zy-liucheng-inner');
        var $i = $parent_inner.find('i[id="1E3290704BF1749EE0530E0AA8C00077"]');
        var $em = $i.siblings('em');
        var value = $.trim($this_input.val());
        if (value) {
            if (value.length < 15) {
                $i.attr('data-name', value);
                $em.attr('title', value);
                if (value.length > 8) {
                    $em.text(value.substr(0, 8) + '...');
                } else {
                    $em.text(value);
                }
            } else {
                value = value.substr(0, 14);
                $i.attr('data-name', value);
                $em.attr('title', value);
                $em.text(value.substr(0, 8) + '...');
                $this_input.TIP({content: '维修流程名称不超过14个字符!', 'scroll': false});
            }
        } else {
            $this_input.TIP({content: '维修流程名称不能为空!', 'scroll': false});
        }
    });

    // 新增流程 - 删除
    $original_line.on('click', 'button.zy-delete-s', function () {
        var $line = $(this).parent('span').parent('p').parent('div').parent('div.zy-liucheng-line');
        $line.fadeOut('fast', function () {
            $line.remove();
        });
        $('#JQ_flow_list_add').attr('data-change', 'true');
    });

    // 新增流程 - 复选
    $original_line.on('click', 'i.chk', function (event) {
        var $this_i = $(this);
        $this_i.toggleClass('active');

        var perform = $this_i.attr('data-perform');
        var change = $this_i.attr('data-change');

        if (perform == 0) {
            $this_i.attr('data-perform', '1');
        } else if (perform == 1) {
            $this_i.attr('data-perform', '0');
        }

        if (change == 'false') {
            $this_i.attr('data-change', 'true');
        } else if (change == 'true') {
            $this_i.attr('data-change', 'false');
        }

        event.stopPropagation();
    });

    // 新增流程 - 创建
    $('#JQ_flow_list_add').click(function () {
        var $this_btn = $(this);
        if ($this_btn.attr('data-change') == 'true') {
            var $line = $this_btn.parent('div.zy-form-line');
            var $template = $('div.zy-original-line');
            var $clone = $template.clone(true).removeClass('zy-original-line').show();
            $line.before($clone);
            $this_btn.attr('data-change', 'false');
        } else if ($this_btn.attr('data-change') == 'false') {
            $this_btn.TIP({
                'content': '上一个维修流程尚未保存!',
                'scroll': false
            });
        }
    });

    // 保存
    var $liucheng = $('div.zy-liucheng-box');
    $liucheng.on('click', 'button.zy-create-s', function () {
        var $this_btn = $(this);
        if ($this_btn.hasClass('disabled')) {
            return;
        }

        var post_data = dataFormat.getFlow($this_btn);
        DyyBase.ajax(defectApi.url_flow_create(), post_data, function (dataset) {
            if (dataset.bool) {
                $this_btn.MSG({
                    'type': 'success',
                    'content': '保存维修流程成功！',
                    'yesBtnAct': function () {
                        DyyBase.pageReload();
                    },
                    'closeBtnAct': function () {
                        DyyBase.pageReload();
                    }
                });
            }
        });
    });

    // 删除
    $liucheng.on('click', 'button.zy-delete-s', function () {
        var $this_btn = $(this);
        var pid = $this_btn.attr('data-pid');
        $this_btn.MSG({
            'type': 'confirm',
            'title': '温馨提示',
            'content': '删除的维修流程不可恢复，是否确认删除！',
            'height': 240,
            'yesBtnTxt': '确认',
            'tag': 'no',
            'yesBtnAct': function () {
                DyyBase.ajax(defectApi.url_flow_delete(), {pd_id: pid}, function (dataset) {
                    if (dataset.bool) {
                        $this_btn.MSG({
                            'type': 'success',
                            'content': '删除维修流程成功！',
                            'yesBtnAct': function () {
                                DyyBase.pageReload();
                            },
                            'closeBtnAct': function () {
                                DyyBase.pageReload();
                            }
                        });
                    }
                });
            }
        });

    });

    // 设置复选
    $liucheng.on('click', 'i.chk', function (event) {
        var $this_i = $(this);
        $this_i.toggleClass('active');
        var perform = $this_i.attr('data-perform');
        var change = $this_i.attr('data-change');

        if (perform == 0) {
            $this_i.attr('data-perform', '1');
        } else if (perform == 1) {
            $this_i.attr('data-perform', '0');
        }

        if (change == 'false') {
            $this_i.attr('data-change', 'true');
        } else if (change == 'true') {
            $this_i.attr('data-change', 'false');
        }

        changed($this_i);

        event.stopPropagation();
    });

    // 是否发生改变
    function changed(obj_i) {
        var changed = false;
        var $parent_label = obj_i.parent().parent().parent('p.zy-liucheng-p').find('label');

        $parent_label.each(function () {
            var $this_label = $(this);
            if ($this_label.find('i').attr('data-change') == 'true') {
                changed = true;
            }
        });

        var $bar = obj_i.parent().parent().parent().siblings('p.zy-liucheng-bar');
        if (changed) {
            $bar.find('button.zy-button-s').removeClass('disabled');
        } else {
            $bar.find('button.zy-button-s').addClass('disabled');
        }
    }

})(jQuery, window, document);